﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Composite.Events;
using Microsoft.Practices.Composite.Wpf.Events;
using RAD.Modules.Scripts.Interfaces;
using RAD.Modules.Scripts.PresentationModels;
using RAD.Modules.Scripts.Interfaces;
using RAD.Infrastructure;
using RAD.Modules.Scripts.PresentationModels;

namespace RAD.Modules.Scripts.Views
{
    public class ConsoleViewPresenter : IConsoleViewPresenter
    {
        private readonly IScriptService _scriptService;
        private IEventAggregator _eventAggregator;

        public IConsoleView View { get; set; }

        public ConsoleViewPresenter(IConsoleView view, 
                                        IScriptService scriptService,
                                        IEventAggregator eventAggregator)
        {
            View = view;           
            _scriptService = scriptService;
            _eventAggregator = eventAggregator;

            _eventAggregator.GetEvent<UpdateConsoleEvent>().Subscribe(UpdateConsole, ThreadOption.UIThread, true);

            //_scriptRun = new ActiveAwareDelegateCommand<string>(ScriptRun);
            //_commandProxy.ScriptRunCommand.RegisterCommand(_scriptRun);
        }

        private void UpdateConsole(string s)
        {
            View.ConsolePresentationModel.Lines.Clear();
            foreach (ConsoleLine line in _scriptService.Output)
            {
                View.ConsolePresentationModel.Lines.Add(line);
            }
            //View.ConsolePresentationModel.Lines = _scriptService.Output;
        }
    }
}
